package com.hmdp.MqListener;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.entity.VoucherOrder;
import com.hmdp.mapper.VoucherOrderMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import static com.hmdp.utils.MqConstance.*;

@Component
@RequiredArgsConstructor
public class OrderListener extends ServiceImpl<VoucherOrderMapper,VoucherOrder> {


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = ORDER_QUEUE, durable = "true"),
            exchange = @Exchange(name = ORDER_EXCHANGE),
            key = ORDER_ROUTING_KEY
    ))
    public void listenPlaceOrder(VoucherOrder voucherOrder){
        System.out.println("收到订单消息："+voucherOrder);
        //再次验证一人一单的有效性
        if(getById(voucherOrder.getId())!=null){
            return;
        }
        //插入订单
        save(voucherOrder);

    }

}
